package com.avic.rong.search.adapter.searchResultFragmentsAdapters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.goodsSort.bean.showRvBean.GoodsSortSearchAllBzBean;
import com.avic.rong.questionOrAnswer.activity.QuestionDetailsActivity;
import com.avic.rong.search.activity.SearchHomePageActivity;
import com.avic.rong.utils.StringUtils;
import com.avic.rong.utils.ToastTools;
import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Chenjiaxin on 2019/7/20 12:55
 * E-Mail Address：16601203965@163.com
 * 收缩版adapter
 */
public class SearchWendaShrinkRvAdapter extends RecyclerView.Adapter<SearchWendaShrinkRvAdapter.SearchPeiXunRvViewHolder> {

    private Context context;
    private LayoutInflater inflater;
    private List<GoodsSortSearchAllBzBean.DataBean> list;
    private static int itemCount = 2;
    private SearchHomePagePeiXunListener listener;

    public SearchHomePagePeiXunListener getListener() {
        return listener;
    }

    public void setListener(SearchHomePagePeiXunListener listener) {
        this.listener = listener;
    }

    public SearchWendaShrinkRvAdapter(Context context, List<GoodsSortSearchAllBzBean.DataBean> list) {
        this.context = context;
        this.list = list;
        this.inflater = LayoutInflater.from(context);
    }

    public void addItemCount(int num) {
        itemCount = num;
    }

    @NonNull
    @Override
    public SearchPeiXunRvViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = inflater.inflate(R.layout.view_command, viewGroup, false);
        return new SearchPeiXunRvViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SearchPeiXunRvViewHolder holder, int i) {
        if (list.get(i).getTitle() != null){
            int startIndex = list.get(i).getTitle().toLowerCase().indexOf(SearchHomePageActivity.nowSearchStr.toLowerCase());
            if (startIndex>=0){
                SpannableString spannableString = new SpannableString(list.get(i).getTitle());
                ForegroundColorSpan colorSpan = new ForegroundColorSpan(ContextCompat.getColor(context, R.color.color_f23030));
                spannableString.setSpan(colorSpan, startIndex, startIndex+SearchHomePageActivity.nowSearchStr.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
                holder.noTvCategoryName.setText(spannableString);
            }else {
                holder.noTvCategoryName.setText(list.get(i).getTitle());
            }
        }
        if (StringUtils.isEmpty(list.get(i).getAnsNum()+"")) {
            holder.noReadsize.setText("回答"+0+"");
        }else{
            Object ansNum = list.get(i).getAnsNum();
            String s = ansNum.toString();
            String substring = s.substring(0, s.length() - 2);
            holder.noReadsize.setText("回答"+substring);
        }

        if (StringUtils.isEmpty(list.get(i).getAnsNum()+"")) {
            holder.noBefore.setText("不详");
        }else{
            String postTime = list.get(i).getPostTime();
            String[] ts = postTime.split("T");
            holder.noBefore.setText(ts[0]);
        }

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    long dataId = list.get(i).getDataId();
                    context.startActivity(QuestionDetailsActivity.queryIntent(context,dataId+""));
                }
            });

    }

    @Override
    public int getItemCount() {
        if (list.size() > 2)
            return itemCount;
        else
            return list.size();
    }

    class SearchPeiXunRvViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.no_tv_category_name)
        TextView noTvCategoryName;
        @BindView(R.id.no_ribao)
        TextView noRibao;
        @BindView(R.id.no_readsize)
        TextView noReadsize;
        @BindView(R.id.no_before)
        TextView noBefore;
        public SearchPeiXunRvViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface SearchHomePagePeiXunListener {
        void searchPeiXunClick(int index);
    }

}
